/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package interfaz.plantillas;

import entidades.DatosInstitucionales;
import entidades.Enumeraciones;
import exceptions.ValoresIncompletosException;
import exceptions.ValoresIncorrectosException;
import interfaces.PlantillaRegistro;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author fcarou
 */
public class PlantillaDatosInstitucionales extends javax.swing.JPanel implements PlantillaRegistro
{
    private DatosInstitucionales datos;

    /**
     * Creates new form PlantillaDatosInstitucionales
     * @param datos
     */
    public PlantillaDatosInstitucionales(DatosInstitucionales datos)
    {
        this.datos = datos;
        initComponents();
    }

    public DatosInstitucionales getDatos()
    {
        return datos;
    }

    public void setDatos(DatosInstitucionales datos)
    {
        this.datos = datos;
    }
    
    private void setCombos ()
    {
        botonSeleccionarPresentante.setEnabled(datos.getCategoria() == Enumeraciones.Categoria.ADHERENTE);
        comboTipo.setEnabled(datos.getCategoria() == Enumeraciones.Categoria.ACTIVO);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jLabel1 = new javax.swing.JLabel();
        comboCategoria = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        comboTipo = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        campoResAlta = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        botonSeleccionarPresentante = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos Institucionales", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.gray));

        jLabel1.setText("Categoria");

        comboCategoria.setModel(new DefaultComboBoxModel(Enumeraciones.Categoria.values()));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${datos.categoria}"), comboCategoria, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        comboCategoria.addItemListener(new java.awt.event.ItemListener()
        {
            public void itemStateChanged(java.awt.event.ItemEvent evt)
            {
                comboCategoriaItemStateChanged(evt);
            }
        });

        jLabel2.setText("Tipo");

        comboTipo.setModel(new DefaultComboBoxModel(Enumeraciones.Tipo.values()));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${datos.tipo}"), comboTipo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        jLabel3.setText("Resolución alta");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${datos.resAlta}"), campoResAlta, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel4.setText("Presentante");

        botonSeleccionarPresentante.setText("Seleccionar");
        botonSeleccionarPresentante.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                botonSeleccionarPresentanteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(comboCategoria, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(comboTipo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(campoResAlta)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 103, Short.MAX_VALUE)
                        .addComponent(botonSeleccionarPresentante)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoResAlta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(botonSeleccionarPresentante))
                .addContainerGap(75, Short.MAX_VALUE))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void botonSeleccionarPresentanteActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_botonSeleccionarPresentanteActionPerformed
    {//GEN-HEADEREND:event_botonSeleccionarPresentanteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_botonSeleccionarPresentanteActionPerformed

    private void comboCategoriaItemStateChanged(java.awt.event.ItemEvent evt)//GEN-FIRST:event_comboCategoriaItemStateChanged
    {//GEN-HEADEREND:event_comboCategoriaItemStateChanged
        Enumeraciones.Categoria categoria = (Enumeraciones.Categoria) comboCategoria.getSelectedItem();
        
        if (categoria == Enumeraciones.Categoria.ACTIVO)
        {
            comboTipo.setEnabled(true);
            botonSeleccionarPresentante.setEnabled(false);
            datos.setPresentante(null);
        }
        else
        {
            if (categoria == Enumeraciones.Categoria.ADHERENTE)
                botonSeleccionarPresentante.setEnabled(true);
            else
            {
                botonSeleccionarPresentante.setEnabled(false);
                datos.setPresentante(null);
            }
            
            comboTipo.setEnabled(false);
            comboTipo.setSelectedItem(Enumeraciones.Tipo.NULL);
            datos.setTipo(Enumeraciones.Tipo.NULL);
        }
    }//GEN-LAST:event_comboCategoriaItemStateChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonSeleccionarPresentante;
    private javax.swing.JTextField campoResAlta;
    private javax.swing.JComboBox comboCategoria;
    private javax.swing.JComboBox comboTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    @Override
    public Object getDato() throws ValoresIncorrectosException, ValoresIncompletosException
    {
        if (datos.getResAlta().split("/").length != 2)
            throw new ValoresIncorrectosException("Debe llenar la res. alta con un número/número.");
        
        if (datos.getCategoria() == Enumeraciones.Categoria.ADHERENTE && datos.getPresentante() == null)
            throw new ValoresIncorrectosException("El socio es adherente, debe seleccionar un presentante.");
        
        if (datos.getCategoria() == Enumeraciones.Categoria.ACTIVO && datos.getTipo() == Enumeraciones.Tipo.NULL)
            throw new ValoresIncorrectosException("El socio es activo, debe seleccionar un tipo.");
        
        if (datos.getCategoria() != Enumeraciones.Categoria.ACTIVO)
            datos.setTipo(Enumeraciones.Tipo.NULL);
        
        return datos;
    }
}
